import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import zhtt.SessionFactoryUtil;
import zhtt.demo01.Customer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mengxiaxia on 2017/4/22.
 */
public class CustomerDao {
    public CustomerDao() {
    }


    @Test
    public void findCustomer() {


        //SqlSessionFactory ssf = null;
        SqlSession sqlSession = null;
        try {
            //ssf = SessionFactoryUtil.getSqlSessionFactoryIntance();
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            System.out.println(sqlSession);



            //List<Customer> customerList = sqlSession.selectList("selectCustomerAll");

            Customer customerBind = new Customer("Man",110,"service");
            List<Customer> customerList = sqlSession.selectList("selectCustomerByBind", customerBind);

            System.out.println("customerList : " + customerList);
            System.out.println("customerList : " + customerList.size());

            List<Customer> customerList1 = new ArrayList<Customer>();
            System.out.println("customerList1 : " + customerList1);
            System.out.println("customerList1 : " + customerList1.size());

            for (Customer customer : customerList) {
                System.out.println(customer);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }


    }

    @Test
    public void selectCustomerAll(){
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            List<Customer> customerList = sqlSession.selectList("selectCustomerAll");
            System.out.println("customerList : " + customerList);
            System.out.println("customerList : " + customerList.size());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Test
    public void selectCustomer3(){
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            List<Integer> list=new ArrayList<Integer>();
            list.add(1);
            list.add(2);
            List<Map<String,Integer>> mapList=new ArrayList<Map<String, Integer>>();
            Map<String,Integer> map=new HashMap<String, Integer>();
            map.put("id",1);
            mapList.add(map);
            List<Customer> customerList = sqlSession.selectList("selectCustomer3",list);
            System.out.println("customerList : " + customerList);
            System.out.println("customerList : " + customerList.size());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Test
    public void selectCustomerByBind(){
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            System.out.println(sqlSession);
            Customer customerBind = new Customer("herry",110,"service");
            List<Customer> customerList = sqlSession.selectList("selectCustomerByBind", customerBind);
            System.out.println("customerList : " + customerList);
            System.out.println("customerList : " + customerList.size());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
    }


    @Test
    public void selectCustomerAll2(){
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            System.out.println(sqlSession);
            Map<Object, Object> customerMap = (Map<Object, Object>) sqlSession.selectMap("selectCustomerAll2", "id");
            System.out.println("customerMap : " + customerMap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Test
    public void saveCustomer() {

        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            Customer customer = new Customer("herry1",110,"service");
            int id = sqlSession.insert("saveCustomer", customer);
            System.out.println(id);
            System.out.println(customer.getId());
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
            sqlSession.close();
        }
    }
}
